package com.moyq5.permit.center.web.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.moyq5.mvc.framework.general.Body;
import com.moyq5.mvc.framework.general.Sorting;
import com.moyq5.mvc.framework.jackson.result.ObjectResult;
import com.moyq5.mvc.framework.jackson.result.StatusResult;
import com.moyq5.mvc.framework.jackson.result.StatusResult.Status;
import com.moyq5.permit.center.bean.embed.SysAdminRoleId;
import com.moyq5.permit.center.bean.entity.SysAdminRole;
import com.moyq5.permit.center.bean.entity.SysRole;
import com.moyq5.permit.center.bean.enums.SysPlatform;
import com.moyq5.permit.center.bean.search.SysAdminRole4Search;
import com.moyq5.permit.center.feign.client.SysAdminRoleFeign;
import com.moyq5.permit.center.feign.client.SysRoleFeign;
import com.moyq5.permit.center.web.jackson.filter.SysAdminRoleFilter;

/**
 * 账号角色信息
 * @author Moyq5
 * @date 2019年7月23日
 */
@RestController
@RequestMapping("/adminRole")
public class SysAdminRoleController extends BaseController {
	
	private static Logger log = LoggerFactory.getLogger(SysAdminRoleController.class);
	
	@Autowired
	private SysAdminRoleFeign adminRoleFeign;
	@Autowired
	private SysRoleFeign roleFeign;
	
	/**
	 * @author Moyq5
	 * @date 2019年7月23日
	 * @param adminRole4s
	 * @param si
	 * @return
	 * @throws JsonProcessingException
	 */
	@GetMapping(value="/list/{plat}/!/**", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
	@ResponseBody
	public String list(SysAdminRole4Search adminRole4s, Sorting si, @PathVariable("plat") int platform) throws JsonProcessingException {
		log.debug("查询账号角色信息>>>list(SysAdminRole4Search, PageData)");
		if (null == adminRole4s.getRole()) {
			adminRole4s.setRole(new SysRole());
		}
		adminRole4s.getRole().setPlatform(SysPlatform.values()[platform]);
		return toJson(adminRoleFeign.list(new Body<SysAdminRole4Search, Sorting>(adminRole4s, si)), SysAdminRoleFilter.get());
	}
	
	/**
	 * 查询账号角色，同时返回角色信息
	 * @author Moyq5
	 * @date 2019年7月23日
	 * @param adminRole4s
	 * @param si
	 * @return
	 * @throws JsonProcessingException
	 */
	@GetMapping(value="/list/role/{plat}/!/**", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
	@ResponseBody
	public String listWithRole(SysAdminRole4Search adminRole4s, Sorting si, @PathVariable("plat") int platform) throws JsonProcessingException {
		log.debug("查询账号角色信息>>>listWithRole(SysAdminRole4Search, PageData)");
		if (null == adminRole4s.getRole()) {
			adminRole4s.setRole(new SysRole());
		}
		adminRole4s.getRole().setPlatform(SysPlatform.values()[platform]);
		return toJson(adminRoleFeign.listWithRole(new Body<SysAdminRole4Search, Sorting>(adminRole4s, si)), SysAdminRoleFilter.getWithRole());
	}
	
	/**
	 * @author Moyq5
	 * @date 2019年7月23日
	 * @param adminRole
	 * @return
	 */
	@PutMapping("/add/{plat}/!/**")
	@ResponseBody
	public ObjectResult<SysAdminRoleId> add(@RequestBody SysAdminRole adminRole, @PathVariable("plat") int platform) {
		log.debug("添加账号角色信息>>>add(SysAdminRole)");
		ObjectResult<SysAdminRoleId> result = new ObjectResult<SysAdminRoleId>();
		Long roleId = adminRole.getId().getRoleId();
		ObjectResult<SysRole> roleRst = roleFeign.one(roleId);
		if (roleRst.getStatus() != Status.OK) {
			result.setFailMessage(roleRst.getMessage());
			return result;
		}
		if (null == roleRst.getData() || roleRst.getData().getPlatform().ordinal() != platform) {
			result.setFailMessage("非法参数");
			return result;
		}
		return adminRoleFeign.create(adminRole);
	}
	
	/**
	 * @author Moyq5
	 * @date 2019年7月23日
	 * @param adminRole
	 * @return
	 */
	@PatchMapping("/edit/{plat}/!/**")
	@ResponseBody
	public StatusResult edit(@RequestBody SysAdminRole adminRole, @PathVariable("plat") int platform) {
		log.debug("更新账号角色信息>>>edit(SysAdminRole)");
		Long roleId = adminRole.getId().getRoleId();
		ObjectResult<SysRole> roleRst = roleFeign.one(roleId);
		if (roleRst.getStatus() != Status.OK) {
			return roleRst;
		}
		StatusResult result = new StatusResult();
		if (null == roleRst.getData() || roleRst.getData().getPlatform().ordinal() != platform) {
			result.setFailMessage("非法参数");
			return result;
		}
		return adminRoleFeign.update(adminRole);
	}
	
	/**
	 * @author Moyq5
	 * @date 2019年7月23日
	 * @param adminRoleId
	 * @return
	 */
	@DeleteMapping("/del/{plat}/!/**")
	@ResponseBody
	public StatusResult del(@RequestBody SysAdminRoleId adminRoleId, @PathVariable("plat") int platform) {
		log.debug("删除账号角色信息>>>del(adminRoleId)");
		Long roleId = adminRoleId.getRoleId();
		ObjectResult<SysRole> roleRst = roleFeign.one(roleId);
		if (roleRst.getStatus() != Status.OK) {
			return roleRst;
		}
		StatusResult result = new StatusResult();
		if (null == roleRst.getData() || roleRst.getData().getPlatform().ordinal() != platform) {
			result.setFailMessage("非法参数");
			return result;
		}
		return adminRoleFeign.remove(adminRoleId);
	}
	
}
